package com.shan.thread;

/**
 * description：<br/>
 * 解决下面这个问题：<br/>
 * 现在有T1、T2、T3三个线程，你怎样保证T2在T1执行完后执行，T3在T2执行完后执行？<br/>
 *
 * @author shanzheng <br/>
 * @date 11:17 2019/1/18.<br/>
 * Modified By <br/>
 */
public class OrderThreadDemo {
    public static void main(String[] args) {
        method1();
        method2();
    }

    /***
     * Description 方法一
     * @author shanzheng
     * date  2019/1/18
     * @return void
     */
    private static void method1() {
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("t1 is finished");
            }
        });
        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    t1.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("t2 is finished");
            }
        });
        Thread t3 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    t2.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("t3 is finished");
            }
        });
        t1.start();
        t2.start();
        t3.start();
    }

    /***
     * Description 方法二
     * @author shanzheng
     * date  2019/1/18
     * @return void
     */
    private static void method2() {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + "执行完成");
            }

        };
        Thread t1 = new Thread(runnable, "t1");
        Thread t2 = new Thread(runnable, "t2");
        Thread t3 = new Thread(runnable, "t3");
        try {
            t1.start();
            t1.join();
            t2.start();
            t2.join();
            t3.start();
            t3.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
